#ifdef _WIN_

namespace Sky
{
LARGE_INTEGER  Freq;
Bool TimeCounter::m_bUsingQPF;
TUint64 TimeCounter::m_llQPFTickesPerSec = 1000;
TUint64 TimeCounter::GetTicksPerSecond(){return TimeCounter::m_llQPFTickesPerSec;}

TimeCounter::TimeCounter():m_llCountedLen(0),m_llUncountedLen(0),m_llElapsedLen(0)
{
	TimeCounter::m_bUsingQPF	=(QueryPerformanceFrequency(&Freq)!=0);
	TimeCounter::m_llQPFTickesPerSec=Freq.QuadPart;
	Reset(vNull);
}
Void	 TimeCounter::Reset(TUint64* ResetTime)
{
	m_llCountedLen=0;
	m_llUncountedLen=0;
	m_llElapsedLen=0;
	if(TimeCounter::m_bUsingQPF)
	{
		LARGE_INTEGER qwTime;
		QueryPerformanceCounter(&qwTime);
		m_llLastStoppedTime		=	0;
		m_llLastResetedTime     =	qwTime.QuadPart;
		m_llLastElapsedTime		=	qwTime.QuadPart;
		if(ResetTime)*ResetTime=qwTime.QuadPart;
	}
}
Void	 TimeCounter::Start(TUint64* StartTime)
{
	if(TimeCounter::m_bUsingQPF)
	{
		LARGE_INTEGER qwTime;
		QueryPerformanceCounter(&qwTime);
		if(m_llLastStoppedTime != 0)
		{
			m_llUncountedLen		+=qwTime.QuadPart-m_llLastStoppedTime;
			m_llLastElapsedTime		+=qwTime.QuadPart-m_llLastStoppedTime;
			m_llLastStoppedTime		=0;
		}
		if(StartTime)*StartTime=qwTime.QuadPart;
	}
}
Void	 TimeCounter::Stop(TUint64* StopTime)
{
	if(TimeCounter::m_bUsingQPF)
	{
		if(m_llLastStoppedTime == 0)
		{
			LARGE_INTEGER qwTime;
			QueryPerformanceCounter(&qwTime);
			m_llLastStoppedTime		=qwTime.QuadPart;
			if(StopTime)*StopTime	=qwTime.QuadPart;
		}
	}
}
TUint64 TimeCounter::GetTicksElapsed(){return m_llElapsedLen;}
TUint64 TimeCounter::GetTicksCounted(){return m_llCountedLen;}
TUint64 TimeCounter::CalTicksCounted()
{
	if(!TimeCounter::m_bUsingQPF)return -1;
	LARGE_INTEGER qwTime;
	if(m_llLastStoppedTime !=0 )//stopped
		qwTime.QuadPart = m_llLastStoppedTime;
	else
		QueryPerformanceCounter(&qwTime);
	m_llCountedLen = qwTime.QuadPart - m_llUncountedLen - m_llLastResetedTime;
	return m_llCountedLen;
}
TUint64 TimeCounter::CalTicksElapsed()
{
	if(!TimeCounter::m_bUsingQPF)return -1;
	LARGE_INTEGER qwTime;
	if(m_llLastStoppedTime != 0)//stopped
		qwTime.QuadPart = m_llLastStoppedTime;
	else 
		QueryPerformanceCounter(&qwTime);
	m_llElapsedLen = qwTime.QuadPart - m_llLastElapsedTime;
	m_llLastElapsedTime = qwTime.QuadPart;
	return m_llElapsedLen;
}
Bool   TimeCounter::IsStopped()
{
	return (m_llLastStoppedTime != 0);
}

Sky::NType::Double TimeCounter::getElapsedTime()
{
	return m_ElapsedTime;
}

}
#endif
